package com.vsd.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.dialet.PaginationSupport;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.shopcart.domain.StoreGoods;
import com.vsd.daoEx.model.IndexActGoodsExWithGoods;
import com.vsd.domain.IndexActGoods;
import com.vsd.service.IndexActGoodsService;
import com.vsd.service.StoreGoodsService;

@Controller
@RequestMapping("indexActGoods")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class IndexActGoodsController extends CoreAdminController{
	@Autowired
	private IndexActGoodsService indexActGoodsService;
	@Autowired
	private StoreGoodsService storeGoodsService;
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		
		PaginationSupport.byPage(start/length+1, length);
		List<IndexActGoodsExWithGoods> list = indexActGoodsService.selectExList(areaCode);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
	@RequestMapping("/listByStore")
	@ResponseBody
	public String listByStore(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String actId,HttpServletRequest request){
		String storeId = getSessionParameter("id", session);
		
		PaginationSupport.byPage(start/length+1, length);
		List<IndexActGoodsExWithGoods> list = indexActGoodsService.selectExList(actId,storeId);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
  
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String string : ids) {
			indexActGoodsService.delById(string);
		}
		return JSONUtils.serialize(new Ret(0));
	}


	@RequestMapping("/update")
	@ResponseBody
	public String update(String id,Integer state,String remark,Integer sortOrder){
		
		IndexActGoods g = new IndexActGoods();
		g.setId(id);
		g.setState(state);
		g.setRemark(remark);
		g.setSortOrder(sortOrder);
		indexActGoodsService.updateByPrimaryKey(g);
		
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(IndexActGoods goods,HttpSession session){
		
		IndexActGoods a = indexActGoodsService.findByGoodsIdAndActId(goods.getGid(), goods.getActId());
		//已申请过
		if(a!=null) return JSONUtils.serialize(new Ret(-1,"请勿重复申请"));
		//商品是否存在
		StoreGoods g = storeGoodsService.findById(goods.getGid());
		if(g==null||!g.getStoreId().equals(getSessionParameter("id", session))) return JSONUtils.serialize(new Ret(-1,"商品不存在"));
		
		goods.setId(StringUtil.getUUID());
		goods.setCreateTime(new Date());
		goods.setSortOrder(0);
		goods.setState(1);
		indexActGoodsService.save(goods);
		return JSONUtils.serialize(new Ret(0));
	}
	
}
